package com.hbk.service.impl;

import com.hbk.base.common.CommonResult;
import com.hbk.base.common.DataGrid;
import com.hbk.base.common.Ref;
import com.hbk.entity.Navigation;
import com.hbk.mapper.NavigationMapper;
import com.hbk.param.NavigationParam;
import com.hbk.service.ICacheService;
import com.hbk.service.INavigationService;
import com.hbk.vo.NavigationVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class NavigationServiceImpl implements INavigationService {

    @Resource
    private NavigationMapper navigationMapper;
    @Resource
    private ICacheService cacheService;

    /**获取列表*/
    public CommonResult list(){
        List<Navigation> navigationList = navigationMapper.listAll();
        int count = navigationList.size();
        List<NavigationVo.ListVo> vos = new ArrayList<>();
        for(Navigation nav : navigationList){
            NavigationVo.ListVo vo = new NavigationVo.ListVo();
            vo.setId(nav.getId());
            vo.setName(nav.getName());
            vo.setUrl(nav.getUrl());
            vos.add(vo);
        }
        DataGrid<NavigationVo.ListVo> grid = new DataGrid<>();
        grid.setRows(vos);
        grid.setTotal(count);
        return Ref.ok(grid);
    }

    /**修改*/
    public CommonResult modify(NavigationParam.ModifyParam param){

        Navigation navigation = navigationMapper.selectById(param.getId());
        if(navigation==null){
            return Ref.error("找不到导航选项");
        }
        navigation.setName(param.getName());
        navigation.setUrl(param.getUrl());
        navigationMapper.updateById(navigation);
        cacheService.deleteNavCache();
        return Ref.ok();
    }
}
